import {shopChangeApply} from '@/api/shop'
import {
  apiUrl
} from '@/api/request'
import { showLoading, showSuccess, showText } from '@/utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    showErcode: false,
    selectArray: [{
      "id": "1",
      "text": "门店照片"
    }],
    uploadList:[],
    applyRemark:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  uploadImage(){
    showLoading()
    wx.chooseMedia({
      count:9,
      mediaType:['image'],
      sourceType: ['album', 'camera'],
      success:(res) =>{
        console.log(res)
        res.tempFiles.map(r=>{
          this.upload(r.tempFilePath)
        })
      }
    })
  },
  upload(detail){
    let url = apiUrl + '/file/uploadFile'
    let obj = {
      src:'',
      width:0,
      height:0
    }
    wx.getImageInfo({
      src: detail,
      success:(res)=>{
        obj.width = res.width
        obj.height = res.height
      }
    })
    // 上传图片
    wx.uploadFile({
      filePath: detail,
      name: 'file',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('access_token')
      },
      url: url,
      success: (res) => {
        let res1 = JSON.parse(res.data)
        console.log(res1.data.url)
        let arr = this.data.uploadList
        obj.src = res1.data.url
        arr.push(obj)
        this.setData({
          uploadList: arr
        })
        console.log(this.data.uploadList)
      },
      fail: (err) => {
        console.log('Url:', url, ' err:', err)
      }
    })
  },
  getInputVal(e){
    this.setData({
      applyRemark:e.detail.value
    })
  },
  submitShopApply(){
    if(this.data.uploadList.length == 0){
      showText('请上传门店照片')
      return
    }
    if(!this.data.applyRemark){
      showText('请输入更新原因')
      return
    }
    shopChangeApply({
      newShopImg:JSON.stringify(this.data.uploadList),
      applyRemark:this.data.applyRemark,
      shopToken:wx.getStorageSync('shopToken')
    }).then(res=>{
      if(res.code == 200){
        showSuccess('提交成功')
        setTimeout(()=>{
          wx.navigateBack({delta:1})
        },1500)
      }else{
        showText(res.msg)
      }
    })
  },
})